#include "diancan_1.h"
#include "ui_diancan_1.h"

diancan_1::diancan_1(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::diancan_1),
    a(nullptr),
    model(nullptr),
    m_sokcket(nullptr),
    row(0),
    sum(0)
{
    ui->setupUi(this);
    resize(500,400);
    a=new QPushButton;
    a->setParent(this);
    a->setText("点餐完毕");
    a->resize(100,30);
    a->move(400,300);

    model = new QStandardItemModel(this);
    model->setColumnCount(3);
    ui->tableView->verticalHeader()->hide();//默认显示行头，如果你觉得不美观的话，我们可以将隐藏
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);//设置选中时为整行选中
    model->setHeaderData(0,Qt::Horizontal, "序号");
    model->setHeaderData(1,Qt::Horizontal, "菜名");
    //model->setHeaderData(ColNum+4,Qt::Horizontal, "等待时间");
    model->setHeaderData(2,Qt::Horizontal, "价格(元)");
    ui->tableView->setModel(model);

    connect(a,SIGNAL(clicked()),this,SLOT(test()));                     //切换窗口
    connect(ui->pushButton_10,SIGNAL(clicked()),this,SLOT(addline()));   //添加菜到列表
   // qDebug()<<"111"<<endl;
    mysql_init();

    cilent_init();
}

diancan_1::~diancan_1()
{
    delete ui;
}

void diancan_1::mysql_init()        //数据库初始化
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("127.0.0.1");
    db.setUserName("root"); //数据库账号
    db.setPassword("123456");//数据库密码
    //db.setPort(3306);
    db.setDatabaseName("restaurtal"); //你的数据库名
    if(!db.open())
    {
        qDebug()<<"连接数据库失败";
    }else
    {
        qDebug()<<"连接数据库成功";
    }
}

void diancan_1::cilent_init()
{
    m_sokcket=new QTcpSocket(this);
    m_sokcket->connectToHost("127.0.0.1",10001);
    //等待连接，时间到了没连接上就自动断开
    m_sokcket->waitForConnected();
}

void diancan_1::test()          //退出菜单
{
    row=0;
    model->clear();
    QString p=QString("有新订单：%1").arg(sum);
    //QArrayData q=QArrayData("有新订单：%1");
    m_sokcket->write(p.toUtf8().data());
    emit mySignal();
}
void diancan_1::huifu()
{
    model->setColumnCount(3);
    model->setHeaderData(0,Qt::Horizontal, "序号");
    model->setHeaderData(1,Qt::Horizontal, "菜名");
    model->setHeaderData(2,Qt::Horizontal, "价格(元)");
    //ui->tableView->setModel(model);
}
void diancan_1::addline()
{
    QSqlQuery query;
    QString sql=QString("create table if not exists "
                        "people_%1 (id int,name varchar(10),price int,over int default '0');").arg(QString::number(sum));
    query.exec(sql);

    QString StrNum = ui->lineEdit->text();     //获取行编辑框文本
    QString StrName = ui->lineEdit_2->text();     //获取行编辑框文本
    model->setItem(row,0,new QStandardItem(QString::number(row)));
    model->setItem(row,1,new QStandardItem(StrNum));
    model->setItem(row,2,new QStandardItem(StrName));
    //执行预处理命令
    QSqlQuery query1;
    QString sql2=QString("insert into people_%1 (id, name, price) values(?,?,?);").arg(QString::number(sum));
    query1.prepare(sql2);
    //给字段设置内容 list
    QVariantList id;
    id<<row;
    QVariantList name;
    name<<StrNum;
    QVariantList price;
    price<<StrName.toInt();
    //给字段绑定相应的值，按顺序绑定
    query1.addBindValue(id);
    query1.addBindValue(name);
    query1.addBindValue(price);
    //执行预预处理命令
    query1.execBatch();
    qDebug()<<query1.lastError().text();
    row++;
    ui->lineEdit->clear();  //清空行编辑框
    ui->lineEdit_2->clear();  //清空行编辑框
}

int diancan_1::danhao()
{
    return sum;
}

int diancan_1::sum_add()
{
    sum=sum+1;
    return sum;
}
void diancan_1::on_pushButton_0_clicked()
{
    ui->lineEdit->setText("鱼香肉丝");
    ui->lineEdit_2->setText("15");
}

void diancan_1::on_pushButton_2_clicked()
{
    ui->lineEdit->setText("宫保鸡丁");
    ui->lineEdit_2->setText("15");
}

void diancan_1::on_pushButton_3_clicked()
{
    ui->lineEdit->setText("油焖大虾");
    ui->lineEdit_2->setText("28");
}

void diancan_1::on_pushButton_4_clicked()
{
    ui->lineEdit->setText("椒盐蘑菇");
    ui->lineEdit_2->setText("12");
}

void diancan_1::on_pushButton_5_clicked()
{
    ui->lineEdit->setText("白斩鸡");
    ui->lineEdit_2->setText("25");
}

void diancan_1::on_pushButton_6_clicked()
{
    ui->lineEdit->setText("小酥肉");
    ui->lineEdit_2->setText("20");
}

void diancan_1::on_pushButton_7_clicked()
{
    ui->lineEdit->setText("米饭");
    ui->lineEdit_2->setText("2");
}

void diancan_1::on_pushButton_8_clicked()
{
    ui->lineEdit->setText("冰峰");
    ui->lineEdit_2->setText("3.5");
}

void diancan_1::on_pushButton_9_clicked()
{
    ui->lineEdit->setText("茶");
    ui->lineEdit_2->setText("免费");
}
